//
//  FilterContentView.m
//  Courses
//
//  Created by xxx on 15/1/20.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "FilterContentView.h"

#import "Teacher.h"

@interface FilterContentView() <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) BOOL displayingTeachers;
@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation FilterContentView

- (NSMutableArray *)items
{
    return _displayingTeachers ? self.teachers : self.tags;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.displayingTeachers = YES;
        
        CGFloat originY = RevFloat(60);
        UIButton *btnConfirm = [[UIButton alloc] initWithFrame:AutoRectMake(40, 0, DEFAULT_WIDTH - 40 * 2, originY, WTFAutoLayoutTypePic)];
        [btnConfirm setTitle:@"确认" forState:UIControlStateNormal];
        btnConfirm.backgroundColor = [UIColor blueColor];
        [btnConfirm addTarget:self action:@selector(doFilter) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnConfirm];
        
        CGRect tableViewFrame = AutoRectMake(120, originY, DEFAULT_WIDTH - 120, RevFloat(frame.size.height - 44), WTFAutoLayoutTypePic);
        _tableView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.clipsToBounds = YES;
        self.clipsToBounds = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
        [self addSubview:_tableView];
        
        _btnShowTeachers = [[UIButton alloc] initWithFrame:AutoRectMake(0, originY, 120, RevFloat(44), WTFAutoLayoutTypePic)];
        [_btnShowTeachers setTitle:@"教师" forState:UIControlStateNormal];
        [_btnShowTeachers setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_btnShowTeachers addTarget:self action:@selector(showTeachers) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btnShowTeachers];
        
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSArray *teachers = [Teacher getTeachers];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.teachers = [teachers mutableCopy];
            });
        });
        
        _selectedTeacherIndex = 0;
        _selectedTagIndex = 0;
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (self.items.count + 1 > indexPath.row)
    {
        if (_displayingTeachers)
        {
            if (_selectedTeacherIndex == indexPath.row) {
                cell.backgroundColor = [UIColor redColor];
            } else {
                cell.backgroundColor = [UIColor whiteColor];
            }
            if(indexPath.row == 0){
                cell.textLabel.text = @"不限";
            }else{
                cell.textLabel.text = [self.items[indexPath.row - 1] TeacherName];
            }
        }
        else
        {
            if (_selectedTagIndex == indexPath.row) {
                cell.backgroundColor = [UIColor redColor];
            } else {
                cell.backgroundColor = [UIColor whiteColor];
            }

        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_displayingTeachers)
    {
        _selectedTeacherIndex = indexPath.row;
    }
    else
    {
        _selectedTagIndex = indexPath.row;
    }
    [_tableView reloadData];
}

- (void)showTeachers
{
    NSLog(@"show teachers:%d",_pageIndex);
    
    self.displayingTeachers = YES;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *teachers = [Teacher getTeachers];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.teachers = [teachers mutableCopy];
            [_tableView reloadData];
        });
    });
}

- (void)showTags
{
    NSLog(@"show tags");
    self.displayingTeachers = NO;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        dispatch_async(dispatch_get_main_queue(), ^{

            [_tableView reloadData];
        });
    });
    [_tableView reloadData];
}

- (void)setTeachers:(NSMutableArray *)teachers
{
    _teachers = teachers;
    if (_displayingTeachers)
    {
        [_tableView reloadData];
    }
}

- (void)setDisplayingTeachers:(BOOL)displayingTeachers
{
    _displayingTeachers = displayingTeachers;
    if (displayingTeachers)
    {
        _btnShowTeachers.backgroundColor = [UIColor redColor];
        _btnShowTags.backgroundColor = [UIColor whiteColor];
    }
    else
    {
        _btnShowTeachers.backgroundColor = [UIColor whiteColor];
        _btnShowTags.backgroundColor = [UIColor redColor];
    }
}

- (void)doFilter
{
    NSString *selectedTeacherId;
    if (_selectedTeacherIndex != 0) {
       selectedTeacherId = [self.teachers[_selectedTeacherIndex - 1] TeacherId];
    }
    NSLog(@"_selectedTeacherIndex:%ld",(long)_selectedTeacherIndex);
    NSLog(@"doFilter with teacher id: %@",selectedTeacherId);
    if (_selectedTeacherIndex == 0) {
        [_delegate doFilterWithTeacherId:0 tagId:0];
    }else{
        [_delegate doFilterWithTeacherId:[self.teachers[_selectedTeacherIndex - 1] TeacherId]
                                   tagId:0];
    }
}

@end
